//
//  PhotoBrowserController.m
//  WisdomZhuHai
//
//  Created by yewei on 13-9-11.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "PhotoBrowser.h"
#import "AppDelegate.h"

@interface PhotoBrowser ()

@property (nonatomic,retain) NSArray *imgPaths;
@property (nonatomic,retain) NSArray *texts;
@property (nonatomic,retain) NSMutableArray *mwPhotos;
@property (nonatomic,retain) NSArray *titles;

-(MWPhotoBrowser *)createBrowser;

@end

@implementation PhotoBrowser
@synthesize imgPaths;
@synthesize texts;
@synthesize mwPhotos;
@synthesize titles;


-(id)initWithImgPaths:(NSArray *)paths texts:(NSArray *)strs titles:(NSArray *)ts
{
    self = [super init];
    if(self)
    {
        self.imgPaths = paths;
        self.texts = strs;
        self.titles = ts;
        
        self.mwPhotos = [NSMutableArray array];
        for(int i = 0; i < [imgPaths count]; ++i)
        {
//            MWPhoto *photo = [[MWPhoto alloc]initWithFilePath:[imgPaths objectAtIndex:i]];
            MWPhoto *photo = [[MWPhoto alloc]initWithURL:[NSURL fileURLWithPath:[imgPaths objectAtIndex:i]]];
            if([self.texts count] > i)
            {
                photo.caption = [self.texts objectAtIndex:i];
            }
            else
            {
                photo.caption = nil;
            }
            photo.captionFont = [UIFont systemFontOfSize:14.0];
            [self.mwPhotos addObject:photo];
            [photo release];
        }

    }
    return self;
}

-(id)initWithImgUrls:(NSArray *)urls texts:(NSArray *)strs titles:(NSArray *)ts
{
    self = [super init];
    if(self)
    {
        self.imgPaths = urls;
        self.texts = strs;
        self.titles = ts;
        self.mwPhotos = [NSMutableArray array];
        for(int i = 0; i < [imgPaths count]; ++i)
        {
            MWPhoto *photo = [[MWPhoto alloc]initWithURL:[NSURL URLWithString:[imgPaths objectAtIndex:i]]];
            if([self.texts count] > i)
            {
                photo.caption = [self.texts objectAtIndex:i];
            }
            else
            {
                photo.caption = nil;
            }
            photo.captionFont = [UIFont systemFontOfSize:14.0];
            [self.mwPhotos addObject:photo];
            [photo release];
        }
        
    }
    return self;

}

-(void)dealloc
{
    [titles release];
    [mwPhotos release];
    [imgPaths release];
    [texts release];
    [super dealloc];
}

-(MWPhotoBrowser *)createBrowser
{
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.displayActionButton = YES;
    browser.customTitles = self.titles;
    browser.shouldAutoHidden = NO;
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.shouldAutoRotation = YES;
    return  [browser autorelease];
}

-(void)presentViewWithRootController:(UIViewController *)control
{
   //    [control.navigationController pushViewController:browser animated:YES];
    [control presentModalViewController:[self createBrowser] animated:YES];
    
}

-(void)pushViewWithNavigationController:(UINavigationController *)control
{
    [control pushViewController:[self createBrowser] animated:YES];
}

#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.mwPhotos.count;
}

- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.mwPhotos.count)
        return [self.mwPhotos objectAtIndex:index];
    return nil;
}

-(void)browserVillDisappear
{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.shouldAutoRotation = NO;
}


@end
